#!/usr/bin/perl

use strict;
use warnings;

use PDF::API2;


print "Content-type: application/pdf\n";
print "Content-disposition: inline; filename=raportti.pdf\n\n";
#print "Content-disposition: attachment; filename=raportti.pdf\n\n";

my $pdf = PDF::API2->new();
my $font = $pdf->corefont('Times', -encode => "UTF8");

my @pages;
push @pages, $pdf->page();
$pages[0]->mediabox('A4');
my ($llx, $lly, $urx, $ury) = $pages[0]->get_mediabox();
my $page_width = $urx - $llx;
my $page_height = $ury - $lly;

my $txt = $pages[0]->text();
$txt->font($font, 16);

my $graph = $pages[0]->gfx();
$graph->linewidth(1);
$graph->linecap(0);
$graph->move(2,$page_height - 2);
$graph->line(2,2);
$graph->stroke();

my $x_margin = 10;
my $y_margin = 10;
my $line_height = 16;

my $left_margin = ($page_width > 400) ? (0.05 * $page_width) : 3;
my $current_line = 0;
my $current_line_top_pos = $page_height - $line_height - $y_margin;

my $odd_line = 0;
while($current_line_top_pos - $line_height >= $y_margin) {
    $current_line++;
    if($current_line % 2) {
	$graph->move($x_margin - 2, $current_line_top_pos);
	$graph->line($x_margin - 2, $current_line_top_pos - $line_height);
	$graph->stroke();
	$graph->close();
    }else {
	$graph->move($x_margin, $current_line_top_pos);
	$graph->line($x_margin, $current_line_top_pos - $line_height);
	$graph->stroke();
	$graph->close();
    } 
    $current_line_top_pos -= $line_height;
    $txt->translate(++$left_margin, $current_line_top_pos);
    $txt->text("$current_line: y-pos: $current_line_top_pos, x-pos: $left_margin ABCDEFG");
}

print $pdf->stringify();
$pdf->saveas("koe.pdf");
